package com.reger.test.core;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.Ordered;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.github.pagehelper.PageInfo;
import com.reger.test.base.model.Base;
import com.reger.test.base.service.BaseService;
import com.reger.test.message.model.Message;
import com.reger.test.message.service.MessageService;
import com.reger.test.user.service.UserService;

@EnableTransactionManagement(proxyTargetClass=true,order=Ordered.HIGHEST_PRECEDENCE)
@SpringBootApplication(scanBasePackages={
		"com.reger.test.user",
		"com.reger.test.base",
		"com.reger.test.message"
		})
public class DataSourceMasterSlaveSwapApplication implements CommandLineRunner {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(DataSourceMasterSlaveSwapApplication.class, args).close();
	}
	
	private static final Logger log = LoggerFactory.getLogger(DataSourceMasterSlaveSwapApplication.class);

	@Autowired
	private UserService userService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private BaseService baseService;

	@Override
	public void run(String... args) throws Exception {
		this.testUser();

		this.testBase();
		this.testMessage();

		this.testUser();
		this.testBase();
		this.testMessage();
	}
	private void testBase() {
		String name="test-name-";
		for (int i = 0; i < 10; i++) {
			baseService.save(name+i );
		}
		PageInfo<Base> page = baseService.findAll(1, 5);
		log.info("{}",page);
	}
	private void testMessage() {
		messageService.createIfNotExists();
		String title="test-title-";
		String context="test-context-";
		for (int i = 0; i < 10; i++) {
			messageService.save(title+i, context+i);
		}
		messageService.save(title, context);
		
		List<Message> messages=new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			messages.add(new Message(i*1000L, title+i, context+i, new Date()));
		}
//		messageService.saves(messages);
		PageInfo<Message> page = messageService.findAll(1, 5);
		log.info("out {}",page);
	}
	private void testUser(){
		/**
		 * 默认在主库中执行操作
		 */
		userService.delAll();
		for (int i = 0; i < 5; i++) {
			final String name = "data-name-"+i;
			final String description = "Hello data source master slave swap !";
			log.info("{}",userService.findFirst());
			userService.save(name, description);
		}
		userService.saves( "批量保存6","批量保存5","批量保存4","批量保存3","批量保存2","批量保存1");
		/**
		 * 默认在主库查询
		 */
		log.info("out {}",userService.findFirst());
		/**
		 * 注解到从库查询
		 */
		log.info("out {}",userService.findFirstBySlave());
		/**
		 * java代码切换到从库查询
		*/
		log.info("out {}",userService.findFirst());
		
		log.info("out {}",userService.findAll());
	}
}
